package math

import(
	"plugin"
	"fmt"
)

var ptrChan chan *plugin.Plugin

func init() {
	ptrChan = make(chan *plugin.Plugin, 3)

	go loadPlugin()
	go loadPlugin()
	go loadPlugin()

}

func loadPlugin(){
	ptr, err := plugin.Open("./.so/math.so")
	if err != nil {
		fmt.Println(ptr)
	}
	ptrChan <- ptr
}

func getPtrChan() (ptr *plugin.Plugin) {
	ptr = <- ptrChan
	ptrChan <- ptr
	return
}

func Add(num1 int, num2 int) (num3 int) {
	ptr := getPtrChan()

	Add, _ := ptr.Lookup("Add")
	num3 = Add.(func(int, int) int)(num1, num2)
	return
}

func Reduce(num1 int, num2 int) (num3 int) {
	ptr := getPtrChan()

	Reduce, _ := ptr.Lookup("Reduce")
	num3 = Reduce.(func(int, int) int)(num1, num2)
	return
}
